﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ShockwaveFlashObjects;
using System.IO;

namespace TestSplashScreen
{

    public partial class fSplashScreen : Form
    {
        const int DEFAULT_TIME = 1000;

        Thread t;

       public fSplashScreen()
        {
            InitializeComponent();
            this.Shown += new EventHandler(fSplashScreen_Shown);
            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
            this.closeButton.Click += new EventHandler(closeButton_Click);
        }

       void closeButton_Click(object sender, EventArgs e)
       {
           this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
           this.Close();
       }

        void fSplashScreen_Shown(object sender, EventArgs e)
        {
            string direc = Directory.GetCurrentDirectory();
            direc =direc + @"\CIFP.swf";
            Flash1.LoadMovie(0, direc);
            Flash1.Play();
            initvalues();
            t = new System.Threading.Thread(initApplication);
            t.Start();
        }

        void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (t != null && t.IsAlive)
            {
                t.Abort();
                t = null;
            }
        }

        private void initvalues()
        {
            messageLabel.Text = "Initializing...";
        }

        public void initApplication()
        {
            Thread.Sleep(DEFAULT_TIME);
            this.Invoke((MethodInvoker)(() => setMessage("Searching for updates...")));
            Thread.Sleep(DEFAULT_TIME);
            this.Invoke((MethodInvoker)(() => setMessage("Connectiong to database...")));
            Thread.Sleep(DEFAULT_TIME);
            this.Invoke((MethodInvoker)(() => setMessage("Connectiong to webservices...")));
            Thread.Sleep(DEFAULT_TIME);
            this.Invoke((MethodInvoker)(() => setMessage("Loading settings...")));
            Thread.Sleep(DEFAULT_TIME);
            this.Invoke((MethodInvoker)(() => setMessage("Loading user preferences...")));
            Thread.Sleep(DEFAULT_TIME);
            this.Invoke((MethodInvoker)(() => setMessage("Starting application...")));
            Thread.Sleep(DEFAULT_TIME);
            if (this.InvokeRequired) this.Invoke(new Action(finishProcess));
        }

        public void setMessage(string msg)
        {
            messageLabel.Text = msg;
        }

        private void finishProcess()
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void closeButton_Click_1(object sender, EventArgs e)
        {

        }

    }
}
